package start.from.scratch.love.common.util;

import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.Statement;

public class SQLFormatter {
    public static String sqlFormat(String sql, boolean format) {
        if (!format) {
            return sql;
        }
        // 将多个空格替换为一个空格
        sql = sql.replaceAll("\\s+", " ");

        // 将关键字前后添加换行符
        return sql.replaceAll("(?i)(SELECT|FROM|WHERE|AND|OR|ORDER BY|GROUP BY)", "\n$1");
    }

    public static void main(String[] args) {
        String sql = "SELECT * FROM users WHERE age > 18 AND name = 'John' ORDER BY id";
        String formattedSql = sqlFormat(sql, true);
        System.out.println(formattedSql);
    }
}
